package com.ril;

import com.ril.financial.model.TransactionCollection;
import com.ril.financial.parsers.HsbcCreditParser;
import com.ril.financial.parsers.HsbcDebitParser;
import com.ril.financial.parsers.Parser;
import com.ril.financial.writers.TransactionListWriter;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;

public class FinancialParser {
    private String srcFile;
    private String destFile;
    private String parserType;
    protected static final String HSBC_CREDIT = "HsbcCredit";
    private static final String HSBC_DEBIT = "HsbcDebit";

    public FinancialParser(String srcFile, String destFile, String parserType) {

        this.srcFile = srcFile;
        this.destFile = destFile;
        this.parserType = parserType;
    }

    private static Options createOptions() {

        Options options = new Options();

        Option srcFile = OptionBuilder.withArgName("src").hasArg().withDescription("source file").isRequired().create("srcfile");

        Option destFile = OptionBuilder.withArgName("dest").hasArg().withDescription("dest file").isRequired().create("destfile");

        Option type = OptionBuilder.withArgName("type").hasArg().withDescription("type file").isRequired().create("type");
        options.addOption(srcFile);
        options.addOption(destFile);
        options.addOption(type);

        return options;
    }

    public static void main(String[] args) {

        CommandLineParser parser = new GnuParser();
        try {

            CommandLine line = parser.parse(createOptions(), args);
            FinancialParser financialParser =
                new FinancialParser(line.getOptionValue("srcfile"), line.getOptionValue("destfile"), line.getOptionValue("type", HSBC_CREDIT));

            financialParser.convert();
        } catch (org.apache.commons.cli.ParseException e) {
            printCliHelp("Error in parsing arguments:\n" + e.getMessage());
        }
    }

    public void convert() {

        Parser parser = buildParser();
        TransactionCollection transactions = parser.getTransactions();
        TransactionListWriter writer = new TransactionListWriter(transactions);
        writer.writeToFile(destFile);
    }

    private Parser buildParser() {
        Parser parser = null;
        if (HSBC_CREDIT.equals(parserType)) {
            parser = new HsbcCreditParser(srcFile);
        } else if (HSBC_DEBIT.equals(parserType)) {
            parser = new HsbcDebitParser(srcFile);
        }
        return parser;
    }

    /*
    * private method to output the command line options help
    */
    private static void printCliHelp(String message) {
        System.out.println(message);
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("java -jar FinancialParser.jar", createOptions());
        System.exit(-1);
    }
}
